﻿using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.SpecialUtils.Html;
using System;
using System.Collections.Generic;

namespace SinaImgUpload.SainTool.LocalTool
{
    internal class ClearNoneSinaImg
    {
        public void ClearFloder(string FloderPath)
        {
            List<string> FoloderList = FloderNameHelper.GetFullFileNameList(FloderPath, "*.html");
            foreach (string line in FoloderList)
            {
                Console.WriteLine(line);
                ClearSingleFile(line);
            }
        }

        public void ClearSingleFile(string FilePath)
        {
            string NewFilePath = FileNameHelper.CreateFileNameWithChildFloder(FilePath, "去除非SinaImg", true);
            string Content = FileHelper.ReadAllText(FilePath);
            List<string> ImgHtml = HtmlHelper.GetHtmlValueList(Content, "//img");
            foreach (var item in ImgHtml)
            {
                string ImgLink = HtmlHelper.GetFirstSrc(item, "//img");
                Console.WriteLine(ImgLink);
                if (ImgLink.IndexOf("sinaimg.cn") < 0)
                {
                    Console.WriteLine("非SinaImg{0},时间{1}", ImgLink, DateTime.Now.ToString());
                    Content = Content.Replace(item, "");
                    Content = Content.Replace(ImgLink, "");
                    continue;
                }
                Content = Content.Replace(item, HtmlCodeCreate.LableImg(ImgLink));
            }

            FileHelper.WriteText(NewFilePath, Content);
        }
    }
}